﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SpeedTestReports.XAMLReports;

namespace SpeedTestReports
{
    public partial class RegistrationForm : ChildWindow
    {
        public RegistrationForm()
        {
            InitializeComponent();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void RegisterButton_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateInput())
            {
                this.Cursor = Cursors.Wait;
                XAMLReportsSoapClient client = new XAMLReportsSoapClient();
                client.RegisterUserCompleted += new EventHandler<RegisterUserCompletedEventArgs>(client_RegisterUserCompleted);
                client.RegisterUserAsync(txtName.Text, txtEmail.Text, txtUserName.Text, txtPassword.Password, txtConfirmPwd.Password);
            }
        }

        void client_RegisterUserCompleted(object sender, RegisterUserCompletedEventArgs e)
        {
            this.Cursor = Cursors.Arrow;

            if (e.Result.ToUpper().Equals("SUCCESS"))
            {
                MessageBox.Show("Registration Complete. You may log in with your new user name.");
                this.Close();
            }
            else
            {
                MessageBox.Show(e.Result);
            }
        }

        private bool ValidateInput()
        {
            if (!ValidateText(txtName.Text, "Full Name")){ return false; }
            if (!ValidateText(txtEmail.Text, "Email")) { return false; }
            if (!ValidateText(txtUserName.Text, "User Name")) { return false; }
            if (!ValidateText(txtPassword.Password, "Password")) { return false; }
            if (!ValidateText(txtConfirmPwd.Password, "Confirm Password")) { return false; }

            if (!txtPassword.Password.Equals(txtConfirmPwd.Password))
            {
                MessageBox.Show("Passwords do not match. Please try again.");
                return false; 
            }

            if (!ValidateEmail(txtEmail.Text, "Email")) { return false; }

            return true;
        }

        private bool ValidateText(string text, string boxName)
        {
            if (String.IsNullOrEmpty(text)) 
            {
                MessageBox.Show(boxName + " cannot be left blank. Please try again.");
                return false; 
            }

            return true;
        }

        private bool ValidateEmail(string text, string boxName)
        {
            if (String.IsNullOrEmpty(text))
            {
                MessageBox.Show(boxName + " is not valid. Please try again.");
                return false;
            }

            return true;
        }
    }
}

